﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Text;

namespace Purdue.Extensions
{
    public static class IOExtensions
    {
        [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0", Justification = "NullReferenceException for self is more natural than returning ArguementNullException after validation")]
        public static bool ContainsFile(this DirectoryInfo self, FileInfo fileInfo, bool recursive)
        {
            if (fileInfo == null)
                return false;

            if (recursive)
            {
                var directoryPath = self.FullName.TrimEnd(new[] { Path.DirectorySeparatorChar }) + Path.DirectorySeparatorChar;
                return fileInfo.FullName.StartsWith(directoryPath, StringComparison.OrdinalIgnoreCase);
            }

            return fileInfo.Directory.FullName == self.FullName;
        }

        public static bool ContainsPath(this DirectoryInfo self, string path, bool recursive)
        {
            return self.ContainsFile(new FileInfo(path), recursive);
        }
    }
}
